﻿using System;
using System.Collections.Generic;
using Templates;
using Templates.SharpParts.Logic.Model.Enum;
using Templates.SharpParts.Shells;
using WebService.Nafania.ViewLayer.Model;
using WebService.Nafania.ViewLayer.Model.Base;

namespace WebService.Nafania.ViewLayer.BussinessLayer
{
	public abstract class T4TemplateBaseView
	{
		#region TemplateView Implementation

		private class PropertyShellView : T4TemplateBaseView
		{
			protected override TemplateView[] GetViews()
			{
				PropertyShell block = (PropertyShell) Block;
				TemplateView[] result = new[]
				                        	{
				                        		TemplateView.AsHidden("Id", block.Id),
				                        		TemplateView.AsListText("ShellNamespaces", block.ShellNamespaces),
				                        		TemplateView.AsList("Accessor", block.Accessor),
				                        		TemplateView.AsText("PropertyName", block.PropertyName),
				                        		TemplateView.AsList("PropertyType", block.PropertyType),
				                        	};

				return result;
			}

			protected override void UpdateValues(KeyValuePair[] pairs)
			{
				PropertyShell block = (PropertyShell)Block;
				var id = pairs.GetGuidValueByKey("Id");
				if (block.Id != id)
					throw new ApplicationException("Different Id");

				block.Accessor = (CsAccessor) pairs.GetIntValueByKey("Accessor");
				block.PropertyName = pairs.GetStringValueByKey("PropertyName");
				block.PropertyType = (CsType) pairs.GetIntValueByKey("PropertyType");

				block.ShellNamespaces = pairs.GetListStringValueByBeginOfKey("ShellNamespaces");
			}
		}

		private class ClassShellView : T4TemplateBaseView
		{
			protected override TemplateView[] GetViews()
			{
				ClassShell block = (ClassShell) Block;
				TemplateView[] result = new[]
				                        	{
				                        		TemplateView.AsHidden("Id", block.Id),
				                        		TemplateView.AsList("Accessor", block.Accessor),
				                        		TemplateView.AsText("BaseClassName", block.BaseClassName),
				                        		TemplateView.AsText("ClassName", block.ClassName),
				                        		TemplateView.AsListText("Interfaces", block.Interfaces),
				                        		TemplateView.AsText("NamespaceName", block.NamespaceName),
				                        		TemplateView.AsListText("ShellNamespaces", block.ShellNamespaces),
				                        	};

				return result;
			}

			protected override void UpdateValues(KeyValuePair[] pairs)
			{
				throw new NotImplementedException();
			}
		}

		public class UsingNamespaceShellView : T4TemplateBaseView
		{
			protected override TemplateView[] GetViews()
			{
				UsingNamespaceShell block = (UsingNamespaceShell) Block;
				TemplateView[] result = new[]
				                        	{
				                        		TemplateView.AsHidden("Id", block.Id),
				                        		TemplateView.AsListText("ShellNamespaces", block.ShellNamespaces),
				                        	};

				return result;
			}

			protected override void UpdateValues(KeyValuePair[] pairs)
			{
				throw new NotImplementedException();
			}
		}

		public class TempateCollectionShellView : T4TemplateBaseView
		{
			protected override TemplateView[] GetViews()
			{
				TempateCollectionShell block = (TempateCollectionShell) Block;
				TemplateView[] result = new[]
				                        	{
				                        		TemplateView.AsHidden("Id", block.Id),
				                        		TemplateView.AsListText("ShellNamespaces", block.ShellNamespaces),
				                        		TemplateView.AsListText("Collection", block.Collection),
				                        	};

				return result;
			}

			protected override void UpdateValues(KeyValuePair[] pairs)
			{
				throw new NotImplementedException();
			}
		}

		#endregion

		#region Protected

		protected T4TemplateBase Block;
		protected abstract TemplateView[] GetViews();
		protected abstract void UpdateValues(KeyValuePair[] pairs);

		#endregion

		#region Instance

		private static T4TemplateBaseView Instance(T4TemplateBase block)
		{
			T4TemplateBaseView result = null;

			if (block is PropertyShell)
			{
				result = new PropertyShellView();
			}
			else if (block is ClassShell)
			{
				result = new ClassShellView();
			}
			else if (block is UsingNamespaceShell)
			{
				result = new UsingNamespaceShellView();
			}
			else if (block is TempateCollectionShell)
			{
				result = new TempateCollectionShellView();
			}

			if (result == null)
				throw new NotImplementedException();

			result.Block = block;
			return result;
		}

		#endregion

		public static IEnumerable<TemplateView> GetViews(T4TemplateBase block)
		{
			return Instance(block).GetViews();
		}

		public static void UpdateValues(T4TemplateBase block, KeyValuePair[] pairs)
		{
			Instance(block).UpdateValues(pairs);
		}
	}
}